package com.qdu.plantsearch.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qdu.plantsearch.pojo.Plant;
import com.qdu.plantsearch.service.PlantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Controller
public class PlantController {
    @Autowired
    PlantService plantService;
//植物列表
    @GetMapping("/plant/list")
    public String plantList(Model model){
        Page<Plant> pageParam = new Page<>(1,9);
        plantService.page(pageParam, new QueryWrapper<Plant>().orderByDesc("id"));
        //结果
        List<Plant> plantList = pageParam.getRecords();
        model.addAttribute("plantList",plantList);
        model.addAttribute("pageParam", pageParam);
        return "plant/list";
    }
    @GetMapping("/plant/{page}/{limit}")
    public String plantListPage(@PathVariable int page, @PathVariable int limit, Model model){
        if (page < 1){
            page = 1;
        }
        Page<Plant> pageParam = new Page<>(page, limit);
        plantService.page(pageParam, new QueryWrapper<Plant>().orderByDesc("id"));
        // 结果
        List<Plant> plantList = pageParam.getRecords();
        model.addAttribute("plantList", plantList);
        model.addAttribute("pageParam", pageParam);

        return "plant/list";
    }

    @GetMapping("plant/{id}")
    public String data(@PathVariable("id") int id, Model model){
        Plant plant = plantService.getOne(new QueryWrapper<Plant>().eq("id",id));
        model.addAttribute("plant",plant);
        return "plant/data";
    }
}

